package com.sanjiahantang.youjiarole.module.join.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.city.models.ICityModel;
import com.sanjiahantang.youjiarole.module.city.models.impl.CityModel;
import com.sanjiahantang.youjiarole.module.common.beans.FileBean;
import com.sanjiahantang.youjiarole.module.common.models.IFileModel;
import com.sanjiahantang.youjiarole.module.common.models.impl.FileModel;
import com.sanjiahantang.youjiarole.module.join.beans.CertificationResponseBean;
import com.sanjiahantang.youjiarole.module.join.models.IJoinModel;
import com.sanjiahantang.youjiarole.module.join.models.JoinModel;
import com.sanjiahantang.youjiarole.module.join.views.CertificationView;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/10/21.
 */

public class CertificationPresenter extends BaseMvpPresenter<CertificationView> {

    private IFileModel fileModel;
    private ICityModel cityModel;
    private IJoinModel joinModel;

    public CertificationPresenter(){
        fileModel = new FileModel();
        cityModel = new CityModel();
        joinModel = new JoinModel();
    }
    public void cardSelect(Activity activity,int type) {
        Intent it = new Intent(Intent.ACTION_PICK);
        it.setType("image/*");
        activity.startActivityForResult(it, type);
    }

    public void doUpload(Context context, Uri uri, String type, String token, String phone, final BaseView.OnLoadListener listener) {
        Bitmap bitmap = null;
        new File(uri.toString());
        try {
            bitmap = Utils.getBitmapFormUri((Activity) context,uri);
            doUpload(context,bitmap,type,token,phone,listener);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void doUpload(Context context, Bitmap bitmap, String type, String token, String phone, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN, token);
        param.put(KEY_FILE_ID,"0");
        param.put(KEY_BUSINESS_ID, phone);
        param.put(KEY_BUSINESS_TYPE_ID,type);
        param.put(KEY_FILE_NAME,System.currentTimeMillis() + ".jpg");
        param.put(KEY_FILE_BASE64_PIC, Utils.bitmapToBase64(bitmap));
        fileModel.doUpoad(context,param, new CallBack<FileBean>() {
            @Override
            public void onSuccess(FileBean response, Context context, Object tag) {
                if (response.getCode() == RESPONSE_CODE_SUC){
                    Utils.showToast(context,"上传成功");
                    listener.onLoad(response.getData().getUrl());
                }
            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    public void loadPersonCityList(Context context, BaseView.OnLoadListener onLoadListener) {
        cityModel.getAllArea(context, onLoadListener);
    }

    public void findUserJoin(Context context,String token,String phone, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,token);
        param.put(KEY_PHONE,phone);
        param.put("roleId","0,1,2,3");
        joinModel.findUserJoin(context,param, new CallBack<CertificationResponseBean>() {
            @Override
            public void onSuccess(CertificationResponseBean response, Context context, Object tag) {
                if (response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getData());
                }

            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    public void saveUserJoin(Context context, Object param, final BaseView.OnLoadListener listener) {
        joinModel.saveUserJoin(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    Utils.showToast(context,response.getMessage());
                    listener.onLoad(response.getMessage());
                }
            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    public void deleteFile(Context context, int id,String token, final BaseView.OnLoadListener onLoadListener){
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,token);
        param.put("fileId",String.valueOf(id));
        mView.showProgress();
        fileModel.deleteFileRole(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    Utils.showToast(context,response.getMessage());
                    onLoadListener.onLoad(response.getMessage());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null){
            mView.hideProgress();
        }
    }
}
